package chapter.android.aweme.ss.com.homework;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import chapter.android.aweme.ss.com.homework.model.Message;
import chapter.android.aweme.ss.com.homework.widget.CircleImageView;

public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.MsgViewHolder> {

    private static final String TAG = "zhaijiayu";
    List<Message> mMessages;

    static class MsgViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        TextView time;
        TextView description;
        CircleImageView icon;
        ImageView isOfficial;
        MsgViewHolder(@NonNull View view) {
            super(view);
            title = view.findViewById(R.id.tv_title);
            time = view.findViewById(R.id.tv_time);
            description = view.findViewById(R.id.tv_description);
            icon = view.findViewById(R.id.iv_avatar);
            isOfficial = view.findViewById(R.id.robot_notice);
            Log.d(TAG, "构造函数\n");
            if(title == null){
                Log.d(TAG, "After 构造函数，title is null!\n");
                return;
            }
        }
    }

    MsgAdapter(List<Message> messages){
        Log.d(TAG, messages.toString());
        this.mMessages = messages;
        Log.d(TAG, mMessages.toString());
    }

    @NonNull
    @Override
    public MsgViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        Context context = viewGroup.getContext();
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        //获得视图
        View view = layoutInflater.inflate(R.layout.im_list_item, viewGroup, false);


        Log.d(TAG, "ViewHolder\n");
        if(view.findViewById(R.id.tv_title) == null){
            Log.d(TAG, "获得的视图view里面的内容为空\n");
        }
        else{
            Log.d(TAG, "获得的视图view里面的内容不为空\n");
        }
        return new MsgViewHolder(view);
    }

    private OnItemClickListener listener;
    public interface OnItemClickListener{
        void OnItemClick(View v, int position);
    }
    public void setOnItemClick(OnItemClickListener listener){
        this.listener=listener;
    }
    @Override
    public void onBindViewHolder(@NonNull MsgViewHolder viewHolder, final int i) {
        Message message = mMessages.get(i);
        viewHolder.title.setText(message.getTitle());
        Log.d(TAG, "after setText\n");
        viewHolder.description.setText(message.getDescription());
        viewHolder.time.setText(message.getTime());
        switch (message.getIcon()){
            case "TYPE_ROBOT":{
                viewHolder.icon.setImageResource(R.drawable.session_robot);
                break;
            }
            case "TYPE_GAME":{
                viewHolder.icon.setImageResource(R.drawable.icon_micro_game_comment);
                break;
            }
            case "TYPE_SYSTEM":{
                viewHolder.icon.setImageResource(R.drawable.session_system_notice);
                break;
            }
            case "TYPE_STRANGER":{
                viewHolder.icon.setImageResource(R.drawable.session_stranger);
                break;
            }
            case "TYPE_USER":{
                viewHolder.icon.setImageResource(R.drawable.icon_girl);
                break;
            }
            default:
                throw new IllegalStateException("Unexpected value: " + message.getIcon());
        }
        if(message.isOfficial())
            viewHolder.isOfficial.setVisibility(View.VISIBLE);
        else viewHolder.isOfficial.setVisibility(View.INVISIBLE);




        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(listener!=null){
                    listener.OnItemClick(view, i);
                }
            }
        });
    }



    @Override
    public int getItemCount() {
        return mMessages.size();
    }


}
